package com.filebackup.business.impl;

import java.util.ArrayList;
import java.util.List;

import com.filebackup.business.LocalFolderService;
import com.filebackup.dao.LocalFileDao;
import com.filebackup.dao.LocalFolderDao;
import com.filebackup.dao.ShareDao;
import com.filebackup.dao.UserDao;
import com.filebackup.util.RootPathUtil;
import com.filebackup.vo.Vo;
import com.filebackup.vo.impl.LocalFileVo;
import com.filebackup.vo.impl.LocalFolderVo;
import com.filebackup.vo.impl.SendShareVo;
import com.filebackup.vo.impl.UserVo;

public class LocalFolderServiceImpl implements LocalFolderService {

	private LocalFolderDao localFolderDao;
	private LocalFileDao localFileDao;
	private ShareDao shareDao;
	private UserDao userDao;

	public List<Vo> findByUpperId(int id) {
		// TODO Auto-generated method stub
		return localFolderDao.findByUpperId(id);
	}

	public List<Vo> findRootFolder(int id) {
		// TODO Auto-generated method stub
		System.out.println("a");
		return localFolderDao.findRootFolder(id);
	}

	public void setLocalFolderDao(LocalFolderDao localFolderDao) {
		this.localFolderDao = localFolderDao;
	}

	public LocalFolderDao getLocalFolderDao() {
		return localFolderDao;
	}

	public String tracePath(int folderId) {
		// TODO Auto-generated method stub
		String path = "";
		if (folderId != 0) {
			LocalFolderVo lvo = (LocalFolderVo) this.getLocalFolderDao()
					.findByPK(folderId);
			while (lvo.getUpperId() != 0) {
				path = lvo.getFolderName() + "\\" + path;
				System.out.println("~~~~~~~~~~" + path);
				lvo = (LocalFolderVo) this.getLocalFolderDao().findByPK(
						lvo.getUpperId());
			}
			path = RootPathUtil.ROOT_PATH + lvo.getFolderName() + "\\" + path;
		} else
			path = RootPathUtil.ROOT_PATH;
		System.out.println("path" + "~~" + RootPathUtil.ROOT_PATH);
		return path;
	}

	public void changeFolderName(int id, String name) {
		// TODO Auto-generated method stub
		LocalFolderVo vo = (LocalFolderVo) this.getLocalFolderDao()
				.findByPK(id);
		vo.setFolderName(name);
		vo.setVersion(vo.getVersion() + 1);
		this.getLocalFolderDao().updateVo(vo);
	}

	public Boolean isNewVersion(int id, int version) {
		// TODO Auto-generated method stub
		LocalFolderVo vo = (LocalFolderVo) this.getLocalFolderDao()
				.findByPK(id);
		if (vo.getVersion() == version)
			return true;
		else
			return false;
	}

	public void deleteFolder(int id) {
		// TODO Auto-generated method stub
		simpleDelete((LocalFolderVo) this.getLocalFolderDao().findByPK(id));
		List<Vo> voList = this.getShareDao().findByFolder(id);
		for (int i = 0; i < voList.size(); i++) {
			this.getLocalFileDao().deleteVo(voList.get(i));
		}
	}

	public void simpleDelete(LocalFolderVo vo) {
		List<Vo> lvoList = this.getLocalFolderDao().findByUpperId(vo.getId());
		List<Vo> fvoList = this.getLocalFileDao().findByFolder(vo.getId());
		for (int i = 0; i < lvoList.size(); i++) {
			simpleDelete((LocalFolderVo) lvoList.get(i));
		}
		for (int i = 0; i < fvoList.size(); i++) {
			this.getLocalFileDao().deleteVo(fvoList.get(i));
		}
		this.getLocalFolderDao().deleteVo(
				this.getLocalFolderDao().findByPK(vo.getId()));
	}

	public void setLocalFileDao(LocalFileDao localFileDao) {
		this.localFileDao = localFileDao;
	}

	public LocalFileDao getLocalFileDao() {
		return localFileDao;
	}

	public int addFolder(LocalFolderVo vo) {
		// TODO Auto-generated method stub
		this.getLocalFolderDao().addVo(vo);
		return this.getLocalFolderDao().findIdByVo(vo);
	}

	public List<Vo> findFolderByUser(int id) {
		// TODO Auto-generated method stub
		return this.getLocalFolderDao().findByOwner(id);
	}

	public List<Vo> findAllSubFolder(int id) {
		// TODO Auto-generated method stub
		LocalFolderVo vo = (LocalFolderVo) this.getLocalFolderDao()
				.findByPK(id);
		List<Vo> voList = new ArrayList<Vo>();
		List<Vo> lvoList = this.getLocalFolderDao().findByUpperId(vo.getId());
		for (int i = 0; i < lvoList.size(); i++) {
			voList.addAll(findAllSubFolder(((LocalFolderVo) lvoList.get(i))
					.getId()));
		}
		voList.add(vo);
		return voList;
	}

	public int findNewFolder(LocalFolderVo vo) {
		// TODO Auto-generated method stub
		return this.getLocalFolderDao().findIdByVo(vo);
	}

	public void setShareDao(ShareDao shareDao) {
		this.shareDao = shareDao;
	}

	public ShareDao getShareDao() {
		return shareDao;
	}

	public String returnPath(int folderId) {
		// TODO Auto-generated method stub
		String path = "";
		LocalFolderVo lvo = (LocalFolderVo) this.getLocalFolderDao()
		.findByPK(folderId);
		if (folderId != 0) {
			
			while (lvo.getUpperId() != 0) {
				path = lvo.getFolderName() + "\\" + path;
				System.out.println("~~~~~~~~~~" + path);
				lvo = (LocalFolderVo) this.getLocalFolderDao().findByPK(
						lvo.getUpperId());
			}
			path = lvo.getFolderName() + "\\" + path;
			if(path.endsWith("\\")){
				path=path.substring(0, path.length()-1);
			}
		} else
			path = "";
		System.out.println("path" + "~~" + RootPathUtil.ROOT_PATH);
		return path;
	}

	public UserVo findOwnerById(int id) {
		// TODO Auto-generated method stub
		UserVo vo=(UserVo)this.getUserDao().findByPK(((LocalFolderVo)this.getLocalFolderDao().findByPK(id)).getOwnerId());
		return vo;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

}
